﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace Guog.Common
{
    /// <summary>
    /// 提供将对象与XML文件的序列化支持
    /// </summary>
    public class ConfigHelper
    {
        #region 序列化及反序列化
        /// <summary>
        /// 将对象序列化到文件
        /// </summary>
        /// <typeparam name="T">序列化的对象类型</typeparam>
        /// <param name="obj">要序列化的对象实体</param>
        /// <param name="fileName">输出的文件路径</param>
        /// <returns></returns>
        public static bool Serialization<T>(T obj, string fileName) where T : class, new()
        {
            bool result = false;

            if (obj != null && FileHelper.IsValidFileName(fileName))
            {
                fileName = fileName.Trim();
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                FileStream writer = null;
                try
                {
                    writer = new FileStream(fileName, FileMode.Create);

                    serializer.Serialize(writer, obj);
                    result = true;
                }
                catch (Exception ex)
                {
                    throw new Exception("写入文件出错。", ex);
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                    }
                    if (serializer != null)
                    {
                        serializer = null;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool DeSerialization<T>(string fileName, out T obj) where T : class, new()
        {
            bool result = false;
            obj = new T();
            if (FileHelper.IsValidFileName(fileName) && File.Exists(fileName))
            {
                FileStream reader = null;
                XmlSerializer serializer = null;
                try
                {
                    reader = new FileStream(fileName, FileMode.Open);
                    serializer = new XmlSerializer(typeof(T));
                    obj = serializer.Deserialize(reader) as T;
                    if (obj != null)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    throw new InvalidOperationException("读取文件出错", ex);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                    }
                    if (serializer != null)
                    {
                        serializer = null;
                    }
                }
            }

            return result;
        }
        #endregion
    }
}
